<?php

class Maven_ManageCatalogSpr_Model_Mysql4_Managecatalog extends Mage_Core_Model_Mysql4_Abstract
{
    public function _construct()
    {    
        // Note that the managecatalog_id refers to the key field in your database table.
        $this->_init('managecatalogspr/managecatalog', 'managecatalog_id');
        
    }
    public function updatePendingCron()
    {
    	
    	$sql = "UPDATE ".$this->getTable('managecatalogspr/managecatalog')."
    	SET status = 3, content = 'Process Stopped for the reason Sleeped or killed for last 4 hour!' 
    	WHERE update_time <= DATE_SUB('".date('Y-m-d H:i:s')."', INTERVAL 4 HOUR) 
			AND status=0";
    	
    	$con = Mage::getSingleton('core/resource')->getConnection('core_write');
    	$con->query($sql);
    	/*
    	$this->_getWriteAdapter()->update($this->getTable('managecatalog/managecatalog'), 
    	array('status' => 3, 'content' => 'Process Stopped for the reason Sleeped or killed for last 4 hour!'),
    	'update_time <= DATE_SUB('.date("Y-m-d H:i:s").', INTERVAL 4 HOUR) AND status=0');
    	*/
    }
    public function getLast($title) 
    {
    	$this->updatePendingCron();
    	$select = $this->_getReadAdapter()->select()
			->from(array('main_table'=>$this->getTable('managecatalogspr/managecatalog')))
			->where("main_table.title = '".$title."'")
			->order("main_table.managecatalog_id Desc")
			->limitPage(0,1);
		$res = $this->_getReadAdapter()->fetchAll($select);
		if(count($res)>0) {
			return $res[0];
		}else {
			return array();
		}
    }
}
